<?php
/**
 * Created by PhpStorm.
 * User: huangxiaofeng
 * Date: 18/9/4
 * Time: 下午10:44
 */

namespace App\Admin\Extensions;

use Encore\Admin\Admin;


class Examine
{
    protected $id;

    public function __construct($path,$id)
    {
        $this->id = $id;
        $this->path = $path;
    }

    protected function script()
    {
        return <<<SCRIPT

$('.grid-row-examine').on('click', function () {

    var id=$(this).data('id');
    var examine=$(this).data('examine');
    var button = examine=='success'?'通过':'驳回';
    swal({
      title: "审核",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: button,
      closeOnConfirm: true,
      cancelButtonText: "取消",
      preConfirm: function() {
           $.ajax({
            method: 'post',
            url: '{$this->path}/'+ id +'/examine/',
            data: {
                _method:'post',
                _token:LA.token,
                examine:examine
            },
            success: function (data) {
                $.pjax.reload('#pjax-container');
                if (typeof data === 'object') {
                    if (data.status) {
                        swal(data.message, '', 'success');
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            }
        });
      }
    });
});

SCRIPT;
    }

    public function adopt(){
        Admin::script($this->script());
        return "<a href='javascript:void(0);' data-id='{$this->id}' data-examine='success' class='grid-row-examine'><i class='fa fa-check'></i></a>";
    }

    public function reject(){
        Admin::script($this->script());
        return "<a href='javascript:void(0);' data-id='{$this->id}' data-examine='error' class='grid-row-examine'><i class='fa fa-close'></i></a>";
    }

    public function __toString()
    {
        return $this->adopt().$this->reject();
    }

}